package vitos_family;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

class Main 
{
	public static void main(String [] args)
	{
		new VitosFamily().start();
	}
}

class VitosFamily
{
	public void start()
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		
		try 
		{
		// Read the number of the cases.
		int numOfCases = Integer.parseInt(reader.readLine());
		
		for (int count = 0; count < numOfCases; count++)
		{
			// Read the number of relatives and the street number of Strings.
			String [] values = reader.readLine().split(" ");
			
			// Convert number of relatives in first element of streetNum.
			int numOfRelatives = Integer.parseInt(values[0]);
			
			// Some data structure to hold street number of the relatives.
			int [] streetNumbers = new int[numOfRelatives];
			
			for (int sCount = 1; sCount <= numOfRelatives; sCount++)
			{
				int street = Integer.parseInt(values[sCount]);
				
				streetNumbers[sCount - 1] = street;
			}
			vitosDistance(streetNumbers);
		}
		
		} 
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	void vitosDistance(int [] streets)
	{
		// Sort the streets.
		Arrays.sort(streets);
		
		int counter = 0;
		
		int median = streets[streets.length / 2];
		
		for (int count = 0; count < streets.length; count++)
		{
			// Subtract selected distance from its right neighbor.
			int distance = Math.abs(streets[count] - median);
			
			// Add it to counter, which will be the minimum total distance of Vito's house.
			counter += distance;
		}
		
		System.out.println(counter);
	}
}